import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;


class VariableSearcher{
	 private int carLu;
	 private final StringBuilder buffer = new StringBuilder(); // si plus, tant pis;
	 private final BufferedReader b;
	 
	 public VariableSearcher(String nom) {
		 try {
			 b = new BufferedReader(new InputStreamReader(new FileInputStream(nom),"UTF-8"));
		 } catch (IOException e) {
			 throw new IllegalArgumentException("fichier illisible",e);	
		 }
	 } 
	 
	 public LinkedList<Variable> litFichier() {
		 try {
			 LinkedList<Variable> ensemble = new LinkedList<Variable>();
			 
			 do{carLu = b.read();} while(carLu!=':'); //sauter la partie "var:" carLu==:
			 do{
				 carLu=b.read();
				 String vn = readVariable();//distinguer un variable
				 ensemble.add(new Variable(vn));//l'ajouter dans ensemble variable
			 }while(carLu!='.');
	
			 b.close();
			 return ensemble;
		 }
		 catch (IOException e) {
			 try {
				 b.close();
			 } catch (IOException e1) {/* nothing */}	
			 throw new IllegalArgumentException("invalid file",e);
		 }
	 }	
	 
//distinguer variables et lire leurs noms 
     private String readVariable()throws IOException{
 		//pour l instant, on suppose qu' il n'y a pas de "space"(ascii=32) in .txt.
    	 buffer.setLength(0);
    	 while(','!=carLu&&'.'!=carLu){
    		 buffer.append((char)carLu);
    		 carLu=b.read();
    	 }
    	 return buffer.toString();
     }

}
